/*
* Copyright(C) 2009, kesalin All rights reserved.
* 
* File name : UHostUIHandlerEx.h
* Brief     : HostUIHandler ex.
* Version   : 1.0
* Author    : kesalin@gmail.com
* Date      : 2009-6-5
*/

#pragma once

#include "UCommon.h"

namespace LuoUtils {

// UHostUIHandlerEx
class UHostUIHandlerEx :
	public ATL::CComObjectRootEx<ATL::CComSingleThreadModel>,
	public IDocHostUIHandler,
	public IOleCommandTarget {

	ATL::CComPtr<IDocHostUIHandler> m_DefHostUIHandler;
	ATL::CComPtr<IOleCommandTarget> m_DefCommandTarget;

public:

	BEGIN_COM_MAP(UHostUIHandlerEx)
		COM_INTERFACE_ENTRY(IDocHostUIHandler)
		COM_INTERFACE_ENTRY(IOleCommandTarget)
	END_COM_MAP()

public:

	UHostUIHandlerEx()
	{
	}

	~UHostUIHandlerEx()
	{
	}

	/* */

	static HRESULT Create(UHostUIHandlerEx** pp)
	{
		if (pp == 0) {
			return E_INVALIDARG;
		}

		*pp = 0;

		ATL::CComObject<UHostUIHandlerEx>* obj = 0;
		HRESULT hRslt = ATL::CComObject<UHostUIHandlerEx>::CreateInstance(&obj);
		if (FAILED(hRslt)) {
			return hRslt;
		}

		obj->AddRef();

		*pp = static_cast<UHostUIHandlerEx*>(obj);

		return S_OK;
	}

	/* */

	void SetupDefault(
		IDocHostUIHandler* ui,
		IOleCommandTarget* ct)
	{
		m_DefHostUIHandler = ui;
		m_DefCommandTarget = ct;
	}

	/* */

	// IDocHostUIHandler

	STDMETHOD(ShowContextMenu)(DWORD dwID, POINT FAR* ppt, IUnknown FAR* pcmdTarget, IDispatch FAR* pdispReserved)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->ShowContextMenu(dwID, ppt, pcmdTarget, pdispReserved);
		}

		return S_OK;
	}

	STDMETHOD(ShowUI)(DWORD dwID, IOleInPlaceActiveObject FAR* pActiveObject,
		IOleCommandTarget FAR* pCommandTarget, IOleInPlaceFrame  FAR* pFrame,
		IOleInPlaceUIWindow FAR* pDoc)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->ShowUI(dwID, pActiveObject, pCommandTarget, pFrame, pDoc);
		}

		return S_FALSE;
	}

	STDMETHOD(GetHostInfo)(DOCHOSTUIINFO FAR *pInfo)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->GetHostInfo(pInfo);
		}

		return S_OK;
	}

	STDMETHOD(HideUI)(void)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->HideUI();
		}

		return S_OK;
	}

	STDMETHOD(UpdateUI)(void)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->UpdateUI();
		}

		return S_OK;
	}

	STDMETHOD(EnableModeless)(BOOL fEnable)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->EnableModeless(fEnable);
		}

		return S_OK;
	}

	STDMETHOD(OnDocWindowActivate)(BOOL fActivate)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->OnDocWindowActivate(fActivate);
		}

		return S_OK;
	}

	STDMETHOD(OnFrameWindowActivate)(BOOL fActivate)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->OnFrameWindowActivate(fActivate);
		}

		return S_OK;
	}

	STDMETHOD(ResizeBorder)(LPCRECT prcBorder, IOleInPlaceUIWindow FAR* pUIWindow, BOOL fFrameWindow)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->ResizeBorder(prcBorder, pUIWindow, fFrameWindow);
		}

		return S_OK;
	}

	STDMETHOD(TranslateAccelerator)(LPMSG lpMsg, const GUID FAR* pguidCmdGroup, DWORD nCmdID)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->TranslateAccelerator(lpMsg, pguidCmdGroup, nCmdID);
		}

		return E_NOTIMPL;
	}

	STDMETHOD(GetOptionKeyPath)(LPOLESTR FAR* pchKey, DWORD dw)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->GetOptionKeyPath(pchKey, dw);
		}

		return E_FAIL;
	}

	STDMETHOD(GetDropTarget)(IDropTarget* pDropTarget, IDropTarget** ppDropTarget)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->GetDropTarget(pDropTarget, ppDropTarget);
		}

		return S_OK;
	}

	STDMETHOD(GetExternal)(IDispatch** ppDispatch)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->GetExternal(ppDispatch);
		}

		return S_FALSE;
	}

	STDMETHOD(TranslateUrl)(DWORD dwTranslate, OLECHAR* pchURLIn, OLECHAR** ppchURLOut)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->TranslateUrl(dwTranslate, pchURLIn, ppchURLOut);
		}

		return S_FALSE;
	}

	STDMETHOD(FilterDataObject)(IDataObject* pDO, IDataObject** ppDORet)
	{
		if (m_DefHostUIHandler != 0) {
			return m_DefHostUIHandler->FilterDataObject(pDO, ppDORet);
		}

		return S_FALSE;
	}

	// IOleCommandTarget

	STDMETHOD(QueryStatus)(
		const GUID* pguidCmdGroup,
		ULONG       cCmds,
		OLECMD*     prgCmds,
		OLECMDTEXT* pCmdText)
	{
		ATLTRACE("UHostUIHandlerEx::QueryStatus()\n");


		if (m_DefCommandTarget != 0) {
			return m_DefCommandTarget->QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText);
		}

		return E_NOTIMPL;
	}

	STDMETHOD(Exec)(
		const GUID* pguidCmdGroup,
		DWORD       nCmdID,
		DWORD       nCmdExecOpt,
		VARIANTARG* pvaIn,
		VARIANTARG* pvaOut)
	{
		ATLTRACE("UHostUIHandlerEx::Exec()\n");


		if (m_DefCommandTarget != 0) {
			return m_DefCommandTarget->Exec(pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn, pvaOut);
		}

		return E_NOTIMPL;
	}

	/* */

}; // UHostUIHandlerEx

} // namespace LuoUtils

